package com.haoshuang.zhouzhoubang.ui.page.welfare

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.gyf.immersionbar.ktx.immersionBar
import com.haoshuang.zhouzhoubang.MyApp
import com.haoshuang.zhouzhoubang.config.URLConstants
import com.haoshuang.zhouzhoubang.databinding.ActivityPlatformWelfareHomeBinding
import com.haoshuang.zhouzhoubang.model.bean.PlatformWelfareBean
import com.haoshuang.zhouzhoubang.ui.dialog.CallDialog
import com.haoshuang.zhouzhoubang.ui.page.help.detail.HelpDetailActivity
import com.haoshuang.zhouzhoubang.ui.page.welfare.free.FreeOrderWelfareActivity
import com.haoshuang.zhouzhoubang.ui.page.welfare.insure.InsureWelfareActivityActivity
import com.haoshuang.zhouzhoubang.ui.page.welfare.push.PushOrderWelfareActivity
import com.haoshuang.zhouzhoubang.ui.page.welfare.tool.ToolUseRecordActivity
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.convertInt
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.vertical

/**
 *  福利页面
 */
class PlatformWelfareHomeActivity :
    BaseVMActivity<PlatformWelfareHomeViewModel, ActivityPlatformWelfareHomeBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<PlatformWelfareHomeActivity>()
        }
    }

    private val data = arrayListOf<PlatformWelfareBean>()
    private val adapter = PlatformWelfareAdapter(data)

    override fun initView(savedInstanceState: Bundle?) {
        binding.ivBack.click { finish() }
        binding.recyclerView.vertical().adapter = adapter
        adapter.setEmptyData(msg = "暂无福利")
        adapter.isStateViewEnable = true
        adapter.setOnItemClick { view, position, item ->
            if (item == null) return@setOnItemClick
            // 打开对应福利页面
            when {
                item.welfare_name?.contains("工具") == true -> {
                    startPage<ToolUseRecordActivity>(
                        "id" to item.id,
                        "total" to item.number.convertInt(),
                        "surplus" to item.surplus_number.convertInt(),
                        "type" to 2,
                        "title" to "服务工具"
                    )
                }

                item.welfare_name?.contains("服装") == true -> {
                    startPage<ToolUseRecordActivity>(
                        "id" to item.id,
                        "total" to item.number.convertInt(),
                        "surplus" to item.surplus_number.convertInt(),
                        "type" to 1,
                        "title" to "服务工装"
                    )
                }


                item.welfare_name?.contains("保险") == true -> {
                    startPage<InsureWelfareActivityActivity>(
                        "id" to item.id,
                        "total" to item.number.convertInt(),
                        "surplus" to item.surplus_number.convertInt()
                    )
                }

                item.welfare_name?.contains("免") == true -> {
                    startPage<FreeOrderWelfareActivity>(
                        "id" to item.id,
                        "total" to item.number.convertInt(),
                        "surplus" to item.surplus_number.convertInt()
                    )
                }

                item.welfare_name?.contains("推送") == true -> {
                    startPage<PushOrderWelfareActivity>(
                        "id" to item.id,
                        "total" to item.number.convertInt(),
                        "surplus" to item.surplus_number.convertInt()
                    )
                }

                item.welfare_name?.contains("门店开通") == true -> {
                    startPage<ToolUseRecordActivity>(
                        "id" to item.id,
                        "total" to item.number.convertInt(),
                        "surplus" to item.surplus_number.convertInt(),
                        "type" to 3,
                        "title" to "门店开通"
                    )
                }
            }
        }

        binding.llContactService.click {
            // 联系客服
            if (MyApp.systemConfig?.tel.isNullOrBlank()) {
                toast("客服没有留电话,暂时无法拨打")
                return@click
            }

            XPopup.Builder(mActivity)
                .asCustom(CallDialog(mActivity, MyApp.systemConfig?.tel ?: "") {
                    val intent = Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:${MyApp.systemConfig?.tel}"));
                    startActivity(intent);
                })
                .show()
        }

        binding.tvRule.clickWithTrigger {
            startPage<HelpDetailActivity>(
                "title" to "福利规则",
                "url" to "${URLConstants.baseUrl}index/index/agreement?id=29"
            )
        }
    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        vm.getWelfareList()
    }

    override fun startObserve() {
        vm.getWelfareListData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                data.clear()
                adapter.notifyDataSetChanged()
            }
            onSuccess {
                dismissLoading()
                data.clear()
                data.addAll(it ?: mutableListOf())
                adapter.notifyDataSetChanged()
            }
        }
    }

    override fun initImmersionBar() {
        immersionBar {
            transparentStatusBar()
            titleBarMarginTop(binding.flTitleBar)
        }
    }
}